/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "FixedParametersModel.h"

FixedParametersModel::FixedParametersModel(QObject *parent)
:QAbstractTableModel(parent){
}
void FixedParametersModel::setList(QList<Parameter*>  *parametersList) {
    _list = parametersList;
}
int FixedParametersModel::rowCount(const QModelIndex &/*parent*/) const {
    return _list->count();
}
int FixedParametersModel::columnCount(const QModelIndex &/*parent*/) const {
    return 2;
}
QVariant FixedParametersModel::data(const QModelIndex &index, int role) const {
    if(!_list)
        return QVariant();
    const QList<Parameter*> & list = *_list;
    if(!index.isValid())
        return QVariant();
    if(role == Qt::DisplayRole){
        int r = index.row();
        int c = index.column();
        switch(c){
            case 0:return list[r]->objectName();
                   break;
            case 1:return QString::number(list[r]->value());
                   break;
            default:return QVariant();
                   break;
        }
    }
    return QVariant();
}

bool FixedParametersModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    if(!_list)
        return false;
    QList<Parameter*> & list = *_list;
    if(index.isValid() && (role==Qt::EditRole)){
       int r = index.row();
       int c = index.column();
        switch(c){
            case 0:
                   return true;
                   break;
            case 1:list[r]->setValue(value.toString().toDouble());
                   return true;
                   break;
            default:return false;
                   break;
        }
    }
    return false;
}

QVariant FixedParametersModel::headerData(int section,Qt::Orientation orientation, int role) const {
    if(role== Qt::DisplayRole){
        if(orientation == Qt::Horizontal){
            switch(section){
                case 0:return QString("Parameter");
                       break;
                case 1:return QString("Value");
                       break;
                default:return QVariant();
                       break;
            }
        }
    }
    return QVariant();
}

Qt::ItemFlags FixedParametersModel::flags(const QModelIndex &index) const {
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    return flags|Qt::ItemIsEditable;
}
void FixedParametersModel::update(){
    reset();
}
